package com.jiang.jixiang.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.CountryCodeBean;
import com.jiang.jixiang.databinding.ActivityCountryBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.jiang.jixiang.ui.adapter.CountryAdapter;
import com.jiang.jixiang.ui.adapter.CustomItemDecoration;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.newsletter.SideBar;
import com.ws.universal.tools.newsletter.SlideInOutLeftItemAnimator;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

/**
 * 国籍选择-手机通讯录界面
 */
public class CountryActivity extends BaseActivity implements NetWorkListener {
    public static String resultCountyCode = "resultCountyCode";
    private ActivityCountryBinding binding;
    private CountryAdapter countryAdapter;
    private CountryCodeBean countryCodeBean;
    private CustomItemDecoration decoration;
    private LinearLayoutManager layoutManager;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityCountryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    @Override
    protected void initView() {
        binding.inTitle.titleTextTv.setText(R.string.county_title);
        binding.inTitle.titleLeftBtn.setOnClickListener(view -> {
            finish();
        });

        countryAdapter = new CountryAdapter();


        layoutManager = new LinearLayoutManager(getContext());
        binding.rlRecycleView.setLayoutManager(layoutManager);
        //binding.rlRecycleView.addItemDecoration(decoration = new CustomItemDecoration(this));
        binding.rlRecycleView.setItemAnimator(new SlideInOutLeftItemAnimator(binding.rlRecycleView));
        binding.rlRecycleView.setAdapter(countryAdapter);
        stopProgressDialog();
        loadData();
        //initDatas();


        binding.sideBar.setIndexChangeListener(new SideBar.indexChangeListener() {
            @Override
            public void indexChanged(String tag) {
                if (TextUtils.isEmpty(tag) || countryAdapter.getData().size() <= 0) return;
                for (int i = 0; i < countryAdapter.getData().size(); i++) {
                    if (tag.equals(countryAdapter.getData().get(i).getIndexTag())) {
                        layoutManager.scrollToPositionWithOffset(i, 0);
                        //layoutManager.scrollToPosition(i);
                        return;
                    }
                }
            }
        });

        countryAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent = new Intent();
                intent.putExtra(resultCountyCode, countryAdapter.getData().get(position).getArea());
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

    }


    private void loadData() {
        showProgressDialog(false);
        okHttpModel.post(ApiUrl.getCountryCode_Api, null, ApiUrl.getCountryCode_Api_ID, this);
    }


    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.getCountryCode_Api_ID:
                countryCodeBean = GsonUtils.parseJObject(data.toString(), CountryCodeBean.class);
                if (countryCodeBean != null) {
                    //countryAdapter.addData(countryCodeBean.getList());

                    //对数据源进行排序
                    //CommonUtil.sortData(countryCodeBean.getList());
                    //返回一个包含所有Tag字母在内的字符串并赋值给tagsStr
                    //String tagsStr = CommonUtil.getTags(countryCodeBean.getList());
                    //binding.sideBar.setIndexStr(tagsStr);
                    //decoration.setDatas(countryCodeBean.getList(), tagsStr);
                    countryAdapter.addData(countryCodeBean.getList());

                }
                break;
        }


    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }
}
